from django.shortcuts import render
from rest_framework.generics import ListAPIView

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django_redis import get_redis_connection
# 对日志调用
import logging

import Contains
from models import CacheView
from .models import Nav,Category,Banner,Fx,Cate,Categorys,CategoryGoods
from .serializers import NavModelSerializer,CategorySerializer,BannerSerializer,FxSerializer,CateSerializer,CategorysSerializer,CategoryGoodsSerializer

logger = logging.getLogger("django")

# Create your views here.
class HomeAPIView(APIView):
    def get(self,request):
        """测试代码，测试完成以后将来可以删除"""
        # 测试日志功能
        # logger.error("error信息")
        # logger.info("info信息")
        redis = get_redis_connection("sms_code")
        brother = redis.lrange("brother", 0, -1)
        return Response(brother, status.HTTP_200_OK)

# class NavHeaderListAPIView(ListAPIView):
#     queryset = Nav.objects.filter(is_delete=False,is_show=True,position=Contains.Nav_HEADer).order_by("orders")[:Contains.Nav_HEADER_SIZE]
#     serializer_class = NavModelSerializer
# class NavFooterListAPIView(ListAPIView):
#     queryset = Nav.objects.filter(is_delete=False,is_show=True,position=Contains.Nav_Footer).order_by("orders")[:Contains.Nav_FOOTER_SIZE]
#     serializer_class = NavModelSerializer
# class CategoryListAPIView(ListAPIView):
#     # queryset = Category.objects.filter(is_delete=False,is_show=True,ishome=True).order_by("orders")
#     # serializer_class = CategorySerializer
#     queryset=Category.objects.filter(parent=None,is_delete=False,is_show=True,ishome=True).order_by("orders")
#     serializer_class = CategorySerializer

class CategoryDetailAPIView(APIView):
    def get(self,request):
        cid=request.GET.get("id")
        data=Category.objects.filter(id=cid,is_delete=False,is_show=True,ishome=True,parent=None).first()
        ser=CategorySerializer(data)
        return Response({"msg":"ok","data":ser.data,"code":200})
class BannerListAPIView(ListAPIView):
    queryset = Banner.objects.filter(is_delete=False,is_show=True).order_by("orders")[:5]
    serializer_class = BannerSerializer
    # def get(self,request):
    #     all=Banner.objects.filter(is_delete=False,is_show=True).order_by("orders")[:5]
    #     ser=BannerSerializer(all)
    #     return Response({"msg":"ok","data":ser.data,"code":200})

class FxListAPIView(ListAPIView):
    queryset = Fx.objects.filter(is_delete=False,is_show=True).order_by("orders")[:5]
    serializer_class = FxSerializer

class CateAPIView(ListAPIView):
    queryset = Cate.objects.filter(is_delete=False, is_show=True).order_by("orders")
    serializer_class = CateSerializer

class CategorysAPIView(ListAPIView):
    queryset = Categorys.objects.filter(is_delete=False, is_show=True).order_by("orders")
    serializer_class = CategorysSerializer
    # def get(self,request):
    #     cid=request.GET.get("cateid")
    #     data=Category.objects.filter(id=cid,is_delete=False,is_show=True).first()
    #     ser=CategorysSerializer(data)
    #     return Response({"msg":"ok","data":ser.data,"code":200})

class CategorygoodsAPIView(ListAPIView):
    queryset = CategoryGoods.objects.filter(is_delete=False, is_show=True).order_by("orders")[:6]
    serializer_class = CategoryGoodsSerializer
    # def get(self, request):
    #     cid = request.GET.get("id")
    #     data = CategoryGoods.objects.filter(categoods_id=cid, is_delete=False, is_show=True).first()
    #     ser = CategoryGoodsSerializer(data)
    #     return Response({"msg": "ok", "data": ser.data, "code": 200})


















class NavHeaderListAPIView(CacheView):
    queryset = Nav.objects.filter(is_delete=False,is_show=True,position=Contains.Nav_HEADer).order_by("orders")[:Contains.Nav_HEADER_SIZE]
    serializer_class = NavModelSerializer
class NavFooterListAPIView(CacheView):
    queryset = Nav.objects.filter(is_delete=False,is_show=True,position=Contains.Nav_Footer).order_by("orders")[:Contains.Nav_FOOTER_SIZE]
    serializer_class = NavModelSerializer
class CategoryListAPIView(CacheView):
    queryset = Category.objects.filter(parent=None,is_delete=False,is_show=True,ishome=True).order_by("orders")
    serializer_class = CategorySerializer